package com.xbq.exceleditor

import android.app.AlertDialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gyf.immersionbar.ktx.immersionBar
import com.xbq.exceleditor.bean.event.LoginInOtherDeviceEvent
import com.xbq.exceleditor.databinding.ActivityMainBinding
import com.xbq.exceleditor.ui.HomeFragment
import com.xbq.exceleditor.ui.MineFragment
import com.xbq.exceleditor.ui.TemplatesFragment
import com.xbq.exceleditor.ui.VideoCourseFragment
import com.xbq.xbqsdk.component.activity.VBActivity
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


@AndroidEntryPoint
class MainActivity : VBActivity<ActivityMainBinding>() {

    val fragments = mutableListOf<Fragment>()

    override fun useEventBus(): Boolean {
        return true
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {  }
        fragments.add(HomeFragment())
        fragments.add(VideoCourseFragment())
        fragments.add(TemplatesFragment())
        fragments.add(MineFragment())
        binding.viewPager.isUserInputEnabled=false
        binding.viewPager.offscreenPageLimit=fragments.size
        binding.viewPager.adapter = object:FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return fragments.size
            }

            override fun createFragment(position: Int): Fragment {
                return fragments[position]
            }

        }
        binding.nav.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.action_home -> {
                    binding.viewPager.setCurrentItem(0,false)
                }
                R.id.action_videocourse -> {
                    binding.viewPager.setCurrentItem(1,false)
                }
                R.id.action_template -> {
                    binding.viewPager.setCurrentItem(2,false)
                }
                R.id.action_mine -> {
                    binding.viewPager.setCurrentItem(3,false)
                }
                else -> {
                    binding.viewPager.setCurrentItem(0,false)
                }
            }
            true
        }
        binding.nav.selectedItemId = R.id.action_home
    }

    override fun onBackPressed() {
        AlertDialog.Builder(this)
            .setMessage("确定要退出应用")
            .setPositiveButton("退出"){dlg,w->
                dlg.dismiss()
                finish()
            }
            .setNegativeButton("再看看"){dlg,w->
                dlg.dismiss()
            }
            .show()
    }

    fun showTemplateFragment() {
        binding.nav.selectedItemId = R.id.action_template
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoginInOtherDevice(userInfoChanged: LoginInOtherDeviceEvent) {
        AlertDialog.Builder(this)
            .setMessage(R.string.tips_login_in_other_device)
            .setPositiveButton(R.string.i_know) { dlg, which ->
                dlg.dismiss()
            }
            .show()
    }


}
